package com.my12306.user.util.auth.mobile;

import com.my12306.common.util.RedisUtils;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;

/**
 * 短用户认证所在类
 *
 * @Author:苏牧夕
 * @Date:2020/7/18 0:49
 * @Version 1.0
 */

public class SmsCodeAuthenticationProvider implements AuthenticationProvider {
    private final static String rgscode = "registerCode_";
    private RedisUtils redisUtils;
    // 注意这里的userdetailservice ，因为SmsCodeAuthenticationProvider类没有@Component
    // 所以这里不能加@Autowire，只能通过外面设置才行
    private UserDetailsService userDetailsService;

    public SmsCodeAuthenticationProvider(RedisUtils redisUtils) {
        this.redisUtils = redisUtils;
    }


    /**
     * 在这里认证用户信息
     *
     * @param authentication
     * @return
     * @throws AuthenticationException
     */
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        SmsCodeAuthenticationToken authenticationToken = (SmsCodeAuthenticationToken) authentication;
//        String mobile = (String) authenticationToken.getPrincipal();
        String mobile = authentication.getName();
        String smsCode = (String) authenticationToken.getCredentials();
        String uuid = ((SmsCodeAuthenticationToken) authentication).getUuid();
        System.out.println("验证码：" + smsCode + ",手机号：" + mobile + ",uuid:" + uuid);
        System.out.println(rgscode + mobile + "_" + uuid);
        String smsRedisCode = null;
        if (uuid != null) {
            smsRedisCode = (String) redisUtils.get(rgscode + mobile + "_" + uuid);
            ;
        } else {
            smsRedisCode = (String) redisUtils.get(rgscode + mobile);
        }


        if (!smsCode.equals(smsRedisCode)) {
            throw new InternalAuthenticationServiceException("手机验证码不正确!");
        }

        UserDetails user = userDetailsService.loadUserByUsername(mobile);
        if (user == null) {
            throw new InternalAuthenticationServiceException("无法获取用户信息");
        }
        System.out.println(user);
        SmsCodeAuthenticationToken authenticationResult = new SmsCodeAuthenticationToken(user, " ", user.getAuthorities());
        authenticationResult.setDetails(authenticationToken.getDetails());
        System.out.println(authenticationResult.isAuthenticated());
        return authenticationResult;
    }

    public boolean supports(Class<?> authentication) {
        return SmsCodeAuthenticationToken.class.isAssignableFrom(authentication);
    }

    public UserDetailsService getUserDetailsService() {
        return userDetailsService;
    }

    public void setUserDetailsService(UserDetailsService userDetailsService) {
        this.userDetailsService = userDetailsService;
    }

    public RedisUtils getRedisUtils() {
        return redisUtils;
    }

    public void setRedisUtils(RedisUtils redisUtils) {
        this.redisUtils = redisUtils;
    }
}
